package com.suning.sawp.in.controller;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.aircondition.AirConditionConstants;
import com.suning.sawp.dto.aircondition.BrandInfo;
import com.suning.sawp.dto.aircondition.SNItemInfoResponseDTO;
import com.suning.sawp.dto.aircondition.SchemeTempletHouse;
import com.suning.sawp.dto.aircondition.SchemeTempletInfo;
import com.suning.sawp.dto.common.EmployeeInfo;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.exception.MobileException;
import com.suning.sawp.in.filter.RequestUserHolder;
import com.suning.sawp.intf.airCondition.AirConditionService;
import com.suning.sawp.intf.airCondition.CommodityDetailRsfService;
import com.suning.sawp.intf.store.StoreInfoService;
import com.suning.sawp.po.store.StoreInfo;

/**
 * 
 * 促销券后台业务逻辑<br>
 * 〈功能详细描述〉
 *
 * @author 12061818
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/adminPage/airCondition")
public class AirConditionController extends DjBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AirConditionController.class);

    @Autowired
    AirConditionService airConditionService;
    
    @Autowired
    CommodityDetailRsfService commodityDetailRsfService;
    
    @Autowired
    StoreInfoService storeInfoService;

    /**
     * 
     * 查询品牌列表
     *
     * @param principal
     * @return
     * @throws MobileException
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryBrandList.do")
    public MyGsonView queryBrandList(Principal principal) throws MobileException {
        ReturnMsg<List<BrandInfo>> returnMsg = new ReturnMsg<List<BrandInfo>>();
        Object[] args = { principal.getName() };
        LOGGER.info("queryBrandList staffId:{}, schemeCode:{}", args);
        List<BrandInfo> obList = new ArrayList<BrandInfo>();
        List<String> brandList = AirConditionConstants.BRAND_CODE_LIST;
        for (String item : brandList) {
            BrandInfo jo = new BrandInfo();
            jo.setBrandCode(item);
            jo.setBrandName(AirConditionConstants.BRAND_MAP.get(item));
            obList.add(jo);
        }

        returnMsg.setData(obList);
        return setGsonView(returnMsg);
    }

    /**
     * 
     * 查询推荐方案
     *
     * @param principal
     * @return
     * @throws MobileException
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/querySchemeTemplet.do")
    public MyGsonView querySchemeTemplet(Principal principal) throws MobileException {
        ReturnMsg<List<SchemeTempletHouse>> returnMsg = new ReturnMsg<List<SchemeTempletHouse>>();
        Object[] args = { principal.getName() };
        LOGGER.info("querySchemeTemplet staffId:{}, schemeCode:{}", args);
        EmployeeInfo employeeInfo = RequestUserHolder.getRequestUser();
        // 校验店员信息
        if (employeeInfo == null || StringUtils.isBlank(employeeInfo.getBranchCode())) {
            LOGGER.error("querySchemeTemplet storeManDto is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0027);
            return setGsonView(returnMsg);
        }

        String branchCode = "";
        
        StoreInfo storeInfo = storeInfoService.queryBranchCodeBySaleCode(employeeInfo.getBranchCode());
        
        if(storeInfo != null && StringUtils.isNotBlank(storeInfo.getBranchCode())){
            branchCode = storeInfo.getBranchCode();
        }else{
            LOGGER.error("querySchemeTemplet branchCode is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0004);
            return setGsonView(returnMsg);
        }
        
        ReturnMsg<List<SchemeTempletHouse>> result = airConditionService.querySchemeTemplet(
                branchCode, "1");

        if (result != null && ReturnMsg.SUCCESS.equals(result.getRetFlag())) {
            returnMsg.setData(result.getData());
        } else {
            LOGGER.error("querySchemeTemplet  input result fail  for staffid  " + principal.getName());
            returnMsg.setErrorCode(ErrorCodeConstants.ERR_0028);
            returnMsg.setRetFlag(ReturnMsg.FAIL);
            returnMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0028));
        }
        return setGsonView(returnMsg);
    }

    /**
     * 
     * 根据模板ID查询模板详情
     *
     * @param templetId
     * @param principal
     * @return
     * @throws MobileException
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryTempletById.do")
    public MyGsonView queryTempletById(String houseType, String templetId, Principal principal) throws MobileException {
        ReturnMsg<SchemeTempletInfo> returnMsg = new ReturnMsg<SchemeTempletInfo>();
        Object[] args = { principal.getName() };
        LOGGER.info("queryTempletById staffId:{}, schemeCode:{}", args);
        EmployeeInfo employeeInfo = RequestUserHolder.getRequestUser();
        // 校验参数
        if (StringUtils.isBlank(templetId) || StringUtils.isBlank(houseType)) {
            LOGGER.error("queryTempletById templetId is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0001);
            return setGsonView(returnMsg);
        }

        // 校验店员信息
        if (employeeInfo == null) {
            LOGGER.error("queryTempletById storeManDto is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0027);
            return setGsonView(returnMsg);
        }

        ReturnMsg<SchemeTempletInfo> result = airConditionService.queryTempletById(templetId, houseType);

        if (result != null && ReturnMsg.SUCCESS.equals(result.getRetFlag())) {
            returnMsg.setData(result.getData());
        } else {
            LOGGER.error("queryTempletById  input result fail  for staffid  " + principal.getName());
            returnMsg.setErrorCode(ErrorCodeConstants.ERR_0028);
            returnMsg.setRetFlag(ReturnMsg.FAIL);
            returnMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0028));
        }
        return setGsonView(returnMsg);
    }

    @RequestMapping(value = "/submitSchemeTemplet.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnMsg<String> submitSchemeTemplet(@RequestBody SchemeTempletInfo schemeTempletInfo, Principal principal)
            throws MobileException {
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info("submitSchemeTemplet ", schemeTempletInfo);
        EmployeeInfo employeeInfo = RequestUserHolder.getRequestUser();

        // 校验店员信息
        if (employeeInfo == null) {
            LOGGER.error("submitSchemeTemplet storeManDto is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0027);
            return returnMsg;
        }

        String branchCode = "";
        
        StoreInfo storeInfo = storeInfoService.queryBranchCodeBySaleCode(employeeInfo.getBranchCode());
        
        if(storeInfo != null && StringUtils.isNotBlank(storeInfo.getBranchCode())){
            branchCode = storeInfo.getBranchCode();
        }else{
            LOGGER.error("querySchemeTemplet branchCode is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0004);
            return returnMsg;
        }
        
        ReturnMsg<String> result = airConditionService.submitSchemeTemplet(schemeTempletInfo, branchCode);
        if (result == null || !ReturnMsg.SUCCESS.equals(result.getRetFlag())) {
            LOGGER.error("submitSchemeTemplet  input result fail  for staffid  " + principal.getName());
            returnMsg.setErrorCode(ErrorCodeConstants.ERR_0002);
            returnMsg.setRetFlag(ReturnMsg.FAIL);
            returnMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
        }
        return returnMsg;
    }
    
    @RequestMapping(value = "/updateSchemeTemplet.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnMsg<String> updateSchemeTemplet(@RequestBody SchemeTempletInfo schemeTempletInfo, Principal principal)
            throws MobileException {
        ReturnMsg<String> returnMsg = new ReturnMsg<String>();
        LOGGER.info("updateSchemeTemplet ", schemeTempletInfo);
        EmployeeInfo employeeInfo = RequestUserHolder.getRequestUser();

        // 校验店员信息
        if (employeeInfo == null) {
            LOGGER.error("updateSchemeTemplet storeManDto is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0027);
            return returnMsg;
        }
        
        String branchCode = "";
        
        StoreInfo storeInfo = storeInfoService.queryBranchCodeBySaleCode(employeeInfo.getBranchCode());
        
        if(storeInfo != null && StringUtils.isNotBlank(storeInfo.getBranchCode())){
            branchCode = storeInfo.getBranchCode();
        }else{
            LOGGER.error("querySchemeTemplet branchCode is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0004);
            return returnMsg;
        }

        ReturnMsg<String> result = airConditionService.updateSchemeTemplet(schemeTempletInfo,branchCode);
        if (result == null || !ReturnMsg.SUCCESS.equals(result.getRetFlag())) {
            LOGGER.error("updateSchemeTemplet  input result fail  for staffid  " + principal.getName());
            returnMsg.setErrorCode(ErrorCodeConstants.ERR_0002);
            returnMsg.setRetFlag(ReturnMsg.FAIL);
            returnMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
        }
        return returnMsg;
    }
    
    @RequestMapping(value = "/queryProductDb.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnMsg<SNItemInfoResponseDTO> queryProductDb(String commCode, Principal principal)
            throws MobileException {
        ReturnMsg<SNItemInfoResponseDTO> returnMsg = new ReturnMsg<SNItemInfoResponseDTO>();
        LOGGER.info("queryProductDb  :"+ commCode);
        SNItemInfoResponseDTO result  = commodityDetailRsfService.queryProductBD(commCode);
       
        if (result == null ) {
            LOGGER.error("updateSchemeTemplet  input result fail  for staffid  " + principal.getName());
            returnMsg.setErrorCode(ErrorCodeConstants.ERR_0002);
            returnMsg.setRetFlag(ReturnMsg.FAIL);
            returnMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0002));
        }else{
            returnMsg.setData(result);
        }
        return returnMsg;
    }

}
